package com.steak.orm.builder;

import com.steak.orm.tool.StringUtil;

import java.lang.reflect.Field;

/**
 * @author 刘牌
 * @Title:
 * @date 2022/3/513:31
 */
public class DeleteSQLBuilder extends BaseSQLBuilder{

    public String deleteSql(Object obj) throws Exception {
        getTableName(obj);
        sqlBuilder.append(DELETE + FROM).append(tableName).append(WHERE + " 1=1 ");
        Field[] fields = getFields(obj);
        for (Field field : fields) {
            String fieldName = StringUtil.getLastStr(field.toString());
            Object value = obj.getClass().getMethod(getField(fieldName)).invoke(obj);
            if (null != value && !"".equals(value)) {
                sqlBuilder.append(AND).append(fieldName).append("=").append("'").append(value).append("'");
            }
        }
        return sqlBuilder.toString();
    }
}
